package com.lagou.client;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 客户端端启动器
 */
@Slf4j
@Component
public class ClientStarter implements ApplicationRunner {
    /**
     * 上下文初始化完成后启动服务
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 监听服务器状态
        ConnectionManager.getManager().watchServer();
        while (true) {
            // 通过最快响应负载均衡策略获取一个连接
            RpcConsumer consumer = FastBalanceStrategy.getConsumer();
            // 如果获取到连接，则发送消息
            if (consumer != null) {
                long dateMillis = System.currentTimeMillis();
                String message = DateUtil.date(dateMillis) + " : are you ok ?";
                consumer.send(message);
            }
            Thread.sleep(2000);
        }
    }
}
